using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library;
using Framework.Library.Components;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Portal.Data;
using Framework.Library.Components.Security;
using Framework.Library.Components.Tabs;
using Framework.Library.Utilities;
using PortalController=Framework.Library.Components.Portal.PortalController;
using Framework.Module.CMS.Library.Zones;
using Framework.Library.Components.Commands;

namespace Framework.Portal.Administrator.Portals
{
    public partial class PortalCacheKey : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblUpdateStatus.Text = string.Empty;
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            if (!IsPostBack)
            {
                //Fill portals
                dropPortal.Items.Clear();
                PortalController.LoadPortals(dropPortal.Items);
                
                dropPortal.Items.Insert(0, new ListItem("Root", "0"));
                dropPortal.SelectedValue = "0";
                
                //dropPortal.DataSource = PortalController.GetPortalsByLang(AppEnv.GetLanguage());
                //dropPortal.DataBind();
                dropPortal.SelectedValue = AppEnv.PortalId();
                LoadZones();
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            DataCaching data = new DataCaching();
            lstCache.DataSource = data.GetKeys();
            lstCache.DataBind();

            lstCacheByPortalID.DataSource = PortalCacheController.GetPortalCacheByPortalIdAndZoneID(ConvertUtility.ToInt32(dropPortal.SelectedValue), ConvertUtility.ToInt32(dropZone.SelectedValue));
            lstCacheByPortalID.DataBind();

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Portal_CacheInfo info = new Portal_CacheInfo();

            foreach(ListItem item in lstCache.Items)
			{
			    if(item.Selected)
			    {
                    info.PortalID = ConvertUtility.ToInt32(dropPortal.SelectedValue);
			        info.ZoneID = ConvertUtility.ToInt32(dropZone.SelectedValue);
			        info.CacheKey = item.Text;

			        PortalCacheController.AddPortalCache(info);
			    }
			}
            lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            Portal_CacheInfo info = new Portal_CacheInfo();

            foreach (ListItem item in lstCacheByPortalID.Items)
            {
                if (item.Selected)
                {

                    info.PortalID = ConvertUtility.ToInt32(dropPortal.SelectedValue);
                    info.ZoneID = ConvertUtility.ToInt32(dropZone.SelectedValue);
                    info.Portal_Cache_ID = ConvertUtility.ToInt32(item.Value);
                    PortalCacheController.DeletePortalCache(info);
                }
            }
            lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
        }
        protected void LoadZones()
        {
            dropZone.Items.Clear();
            ZoneController.LoadZones(dropZone.Items, dropPortal.SelectedValue);
            dropZone.Items.Insert(0, new ListItem("Root", "0"));
        }

        protected void dropPortal_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadZones();
        }
    }
}